/**
 * 
 */
package com.cnhaimei.vod.logic;

import java.util.ArrayList;
import java.util.HashSet;

import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.cnhaimei.base.TransparentActivity;
import com.cnhaimei.com.DragView;
import com.cnhaimei.com.HListView;
import com.cnhaimei.com.PageManager;
import com.cnhaimei.com.PageWrapper;
import com.cnhaimei.com.HListView.MyTouchListner;
import com.cnhaimei.com.PageManager.PageInfoListener;
import com.cnhaimei.vod.R;
import com.cnhaimei.vod.bean.Song;
import com.cnhaimei.vod.logic.EventsManager.EventsListener;
import com.cnhaimei.vod.net.XVod;
import com.cnhaimei.vod.net.dc.Player;
import com.cnhaimei.vod.util.Const;
import com.cnhaimei.vod.util.Util;

/**
 * 已选歌曲列表
 * 
 * @author jin001.wu@gmail.com <br>
 * @date 2011-7-8
 * 
 */
public class SelSongListActivity extends VODActivity implements
		EventsListener, PageInfoListener, 
		//DropListener,
		OnItemSelectedListener,MyTouchListner {
	private static final int BASE_NUM = 10000000;
	private final static int TYPE_SEL = 1;
	private final static int TYPE_DONE = 2;
	private final static String TAG = "SelSongListActivity";
	private final int PAGE_SIZE = 5;
	private HListView listView;
	private TextView pageInoView;
	private TextView titleView;
	private View curSelView;
	private DragView mDragView;
	

	private LayoutInflater mInflater;

	private AdapterEx mListAdapter;

	private PageManager pageManager;

	private PageWrapper pageWrapper;
//	private int itemLayoutId = R.layout.singed_song_list_item;
	private int itemLayoutId = R.layout.singed_page_items;

	// private ArrayList<Song> lst;

	private boolean isNeting = false;
	private String action;
	private int type;
	private boolean isRequestFirstSinging;
	private int mLongClickPos;
	
	private SparseArray<ArrayList<Song>> mPageCache = new SparseArray<ArrayList<Song>>(10);
	private HashSet<Integer> loadingIntegerSet = new HashSet<Integer>(50);
	private boolean isClickedSinger;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initData();
		if (type == TYPE_SEL) {
			this.setContentView(R.layout.v8_sel_song_list);
			itemLayoutId = R.layout.sel_page_items;
		} else {
			this.setContentView(R.layout.v8_singed_song_list);// (R.layout.sel_song_list);
		}


		pageManager = new PageManager();
		pageManager.setPageButtonView(findViewById(R.id.pre_page),
				findViewById(R.id.next_page), null, null);
		if (Util.DEMO) {
			pageManager.setPageInfo(1, 4, false);
		}
		
		initView();
		
		pageManager.setListener(this);
		
		if (type == TYPE_SEL) {
			XVod.G().registerListener(EventsManager.Event.SELCT_SONGS, this);
			XVod.G().registerListener(EventsManager.Event.SELCT_CHANG, this);
			requestSelSongFirst();
		} else {
			XVod.G().registerListener(EventsManager.Event.DONE_SONGS, this);
			XVod.G().registerListener(EventsManager.Event.DONE_CHANG, this);
		}
		
//		if (type == TYPE_SEL) {
//			requestSong(0, 1);//PAGE_SIZE);
//		} else {
			update();
//		}
			
		showBackBtn(true);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		isClickedSinger = false;
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if (isFinishing()) {
			if (type == TYPE_SEL) {
				XVod.G().unRegisterListener(EventsManager.Event.SELCT_SONGS, this);
				XVod.G().unRegisterListener(EventsManager.Event.SELCT_CHANG, this);
			} else {
				XVod.G().unRegisterListener(EventsManager.Event.DONE_SONGS, this);
				XVod.G().unRegisterListener(EventsManager.Event.DONE_CHANG, this);
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	void requestSelSongFirst() {
		if (type == TYPE_SEL && ! isRequestFirstSinging) {
			isRequestFirstSinging = true;
			XVod.G().requestSelSong(0, 1);
		}
	}

	private void initData() {
		action = getIntent().getAction();
		if (Const.Action.ShowSelSongs.equals(action)) {
			type = TYPE_SEL;
		} else {
			type = TYPE_DONE;
		}

		if (Util.DEMO) {
			pageWrapper = new PageWrapper();
			ArrayList<Song> lst = new ArrayList<Song>(42);
			pageWrapper.setData(lst);
			for (int i = 1; i < 4; i++) {
				for (int j = 1; j < 5; j++) {
					lst.add(Song.createSong("寒江雪" + (i * 100 + j), "郑源郑源"
							+ (i * 100 + j), "高清"));
				}
			}
		}
	}

	private void initView() {
		mInflater = LayoutInflater.from(this);
		pageInoView = (TextView) findViewById(R.id.page_info);
		titleView = (TextView) findViewById(R.id.title_tip);
		listView = (HListView) findViewById(R.id.list);
//		listView.setCacheColorHint(Color.TRANSPARENT);
		listView.setmMyTouchListner(this);
		listView.setOnItemSelectedListener(this);
		if (type == TYPE_SEL) {
			mDragView = (DragView)findViewById(R.id.drag);
			
		   // 取消拖动
//			mDragView.setDropListener(this);
//			listView.setOnItemLongClickListener(this);
		}
		
		if (mListAdapter == null) {
			mListAdapter = new AdapterEx();
			listView.setAdapter(mListAdapter);
//			listView.setOnItemClickListener(mListAdapter);
		}
	}

	private void requestSong(int from, int count) {
		// PageWrapper wrapper = new PageWrapper();
		// // int c = 0;//
		// wrapper.setCurPage(page);
		if (loadingIntegerSet.contains(from)) {
			return;
		}
		loadingIntegerSet.add(from);
		
		if (type == TYPE_SEL) {
			int c = Player.G().getPlaylistCount() - 1;
			if (c <= 0) {
				return ;
			}
			XVod.G().requestSelSong(from, count); // sel 均不取0
		} else {
			int c = Player.G().getDonelistCount();
			 if (c <= 0) {
				 return;
			 }
			XVod.G().requestDoneSong(from, count);
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.goback:
			break;
		default:
			super.onClick(v); // need parent
			break;
		}
	}

	private void update() {
		
		ArrayList<Song> lst = null;
		if (pageWrapper != null) {
			lst = pageWrapper.getData();
			Log.e(TAG, "list size:" + lst.size());
		}

		Player p = Player.G();
		int page = pageManager.getCurrentIndex();
		int total = 0;
		if (type == TYPE_SEL) {
			total = p.getPlaylistCount() - 1;
		} else {
			titleView.setText(String.valueOf(Player.G().getDonelistCount()));
			total = p.getDonelistCount();
		}

//		mListAdapter.setList(lst);
		
		int pageCount = (total > 0 ? total - 1 : 0) / PAGE_SIZE + 1;
		pageManager.setPageInfo(page, pageCount, false);
		pageInoView.setText(getString(R.string.page_info, pageManager
				.getCurrentIndex() + 1, pageCount <= 0 ? 1 : pageCount));
		
		int startPos = 0;
		if (pageCount > 0) { // pageCount 定会大于1
			startPos = BASE_NUM / pageCount * pageCount;
		}
		listView.setSelection(startPos + page);
		mListAdapter.notifyDataSetChanged();

	}
	
	private void refreshPage() {
		if (mListAdapter != null) {
			mListAdapter.notifyDataSetChanged();
		}
	}
	
	private void refreshTitle(PageWrapper wrapper) {
		if (type == TYPE_SEL) {
			if (wrapper != null) {// && wrapper.getCurPage() == 0) {
				ArrayList<Song> lst = null;
				lst = wrapper.getData();
				Log.e(TAG, "page wrapper cur page:" + wrapper.getCurPage());
				if (lst != null && !lst.isEmpty()) {
					Song curSong = lst.get(0);
					if (curSong != null) {
						titleView.setText(curSong.getName() + "   "
								+ curSong.getSingerName());
					} else {
						titleView.setText("");
					}
				} else {
					titleView.setText("");
				}
				// return; // TYPE_SEL 的第一条页不在列表中
			}
		}
	}
	
//	public boolean onItemLongClick(AdapterView<?> parent, View view,
//			int position, long id) {
//		int pos[] = new int[2];
//		mLongClickPos  = position;
////		selectItem(view);
//		view.getLocationInWindow(pos);
//		mDragView.startDrag(view, pos[0], pos[1]);
//		return true;
//	}
	
	
	// 取消拖动
	
//	private Rect rect = new Rect();
//	private int pos[] = new int[2];
	
//	public void onDrop(float x, float y) {
//		clickTime = 0;
//		isDraging = false;
//		
//		View root = listView.getSelectedView();
//		if (root == null || root.getTag() == null
//				|| root.getVisibility() != View.VISIBLE) {
//			return;
//		}
//		ViewHolder vh = (ViewHolder) root.getTag();
//		int index = (((int) (y)) / vh.rows[0].getHeight()) % PAGE_SIZE;
//		
////		int cout = vh.rows.length;
////		for (int i = 0; i < cout; i++) {
////			View v = vh.rows[i];
//////			v.getGlobalVisibleRect(rect);
//////			v.getLocationInWindow(pos);
////			rect.set(pos[0], pos[1], pos[0] + v.getWidth(), pos[1]
////					+ v.getHeight());
////			if (rect.contains((int) x, (int) y)) {
//				int position = index;//listView.getPositionForView(v);
//				if (position != GridView.INVALID_POSITION && mLongClickPos != position) {
//					Log.e(TAG, "position:" + position);
//					int page = pageManager.getCurrentIndex();
//					int oldPos = page * PAGE_SIZE + mLongClickPos + 1;
//					int newPos = page * PAGE_SIZE + position + 1;
//					XVod.G().preSortSong(oldPos , newPos);
//				}
////				break;
////			}
////		}
//	}

	public class AdapterEx extends BaseAdapter implements OnItemClickListener,
			OnClickListener,OnTouchListener {

//		ArrayList<Song> lst = new ArrayList<Song>();
//
//		public void setList(ArrayList<Song> lst) {
//			this.lst = lst;
//		}

		public int getCount() {
//			int rtn = lst == null ? 0 : lst.size();
////					- pageManager.getCurrentIndex() * PAGE_SIZE;
//			if (rtn <= 0) {
//				rtn = 0;
//			}
//
//			return rtn;
			
//			final Player p = Player.G();
//			if (type == TYPE_SEL) {
//				int count = p.getPlaylistCount() - 1;
//				if (count <= 0) {
//					return 0;
//				}
//			} else {
//				int count = p.getDonelistCount();
//				if (count <= 0) {
//					return 0;
//				}
//			}
			
			int total = Math.max(0,pageManager.getTotalPage());
			if (total <= 1) {
				return total;
			}
			return Integer.MAX_VALUE;// lst == null ? 0 : lst.size();
		}

		public Object getItem(int position) {
			return null;//lst.get(position);
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
//			ViewHolder holder = null;
//			if (convertView == null) {
//				//
//				convertView = mInflater.inflate(itemLayoutId, null);
//				holder = new ViewHolder();
//				holder.initView(convertView, this);
//				convertView.setTag(holder);
//			} else {
//				holder = (ViewHolder) convertView.getTag();
//			}
//
//			holder.setValues((Song) getItem(position), position);
//
//			return convertView;
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = mInflater.inflate(itemLayoutId, null);
				convertView.setLayoutParams(new Gallery.LayoutParams(
						Gallery.LayoutParams.FILL_PARENT,
						Gallery.LayoutParams.FILL_PARENT));
				holder = new ViewHolder();
				holder.initView(convertView, this, this);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			int total = Math.max(1,pageManager.getTotalPage());
			int page = position % total;
			ArrayList<Song> lst = readData(page);//mPageCache.get(PAGE_SIZE*position);
			holder.setValues(lst, page);

			return convertView;
		}
		
		public ArrayList<Song> readData(int page) {
			int from = page * PAGE_SIZE;
			if (type == TYPE_SEL) {
				from += 1;
			}
			final ArrayList<Song> lst = mPageCache.get(from);
			
			if (Util.DEMO || Util.TEST) {
				return pageWrapper.getData();
			}
			
//			if (type == TYPE_SEL && isRequestFirstSinging) {
//				return null;
//			}
			if (lst == null || lst.isEmpty()) {
				requestSong(from, PAGE_SIZE);
			}
			return lst;
		}

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			if (isNeting) {
				return;
			}
			// Song r = (Song) getItem(position);
			// if (r != null && "108".equals(r.getNum())) {
			// startActivity(Const.Action.RoomMatched);
			// }

//			selectItem(view);

		}

		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.insert: {
				if ( v.getTag() instanceof Integer) {
					unselectItem();
					int pos = (Integer) v.getTag();
//					int page = pageManager.getCurrentIndex();
//					int index = page * PAGE_SIZE + pos + 1;
					XVod.G().preSortSong(pos + 1 , 1);
					showMyToast("插播歌曲置顶成功");
				}
			}
				break;
			case R.id.del: {
				if (v.getTag() instanceof Integer) {
					unselectItem();
					int pos = (Integer) v.getTag();
//					int page = pageManager.getCurrentIndex();
//					int index = page * PAGE_SIZE + pos + 1;
					XVod.G().deleteSong(pos + 1);
					showMyToast("删除歌曲成功");
				}
			}
				break;
			case R.id.add: {
				if (v.getTag() instanceof Song) {
					Song s = (Song) v.getTag();
					XVod.G().insertSong(s);
				}
			}
			break;
			case R.id.singer: {
				StarSongsActivity.starActivity(SelSongListActivity.this,
						((TextView) v).getTag().toString().trim());
			}
			default:
				break;
			}
		}

		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN: {
				return selectItem(v);
			}

			default:
				break;
			}
			return false;
		}
	}
	
	class ViewHolder {

		public View root;
		// public ListView listView;
		// public SubAdapterEx subAdapterEx;
		public View[] rows = new View[5];

		public ViewHolder() {
		}

		public void initView(View layout, OnClickListener lis, OnTouchListener touch) {
			root = layout;
			rows[0] = root.findViewById(R.id.row1);
			rows[1] = root.findViewById(R.id.row2);
			rows[2] = root.findViewById(R.id.row3);
			rows[3] = root.findViewById(R.id.row4);
			rows[4] = root.findViewById(R.id.row5);

			for (int i = 0; i < rows.length; i++) {
				SubViewHolder subHolder = initSubView(rows[i], lis);
				rows[i].setTag(subHolder);
				rows[i].setOnTouchListener(touch);
//				rows[i].setOnClickListener(lis);
			}
			// listView = (ListView) layout.findViewById(R.id.page_list);
			// subAdapterEx = new SubAdapterEx();
			// listView.setAdapter(subAdapterEx);
			// listView.setCacheColorHint(Color.TRANSPARENT);
			// listView.setOnTouchListener(mOnTouchListener);
		}

		private SubViewHolder initSubView(View layout, OnClickListener lis) {
			SubViewHolder subHolder = new SubViewHolder();
			subHolder.initView(layout, lis);
			return subHolder;
		}

		public void setValues(ArrayList<Song> lst, int page) {
			// subAdapterEx.setList(lst);
			// subAdapterEx.notifyDataSetChanged();
			int songSize = lst == null ? 0 : lst.size();
			for (int i = 0; i < rows.length; i++) {
				if (i < songSize) {
					rows[i].setVisibility(View.VISIBLE);
					setSubValues(lst.get(i), (SubViewHolder) rows[i].getTag(),
							page * PAGE_SIZE + i);
				} else {
					rows[i].setVisibility(View.INVISIBLE);
					setSubValues(null, (SubViewHolder) rows[i].getTag(), page * PAGE_SIZE + i);
				}
			}
		}

		private void setSubValues(Song s, SubViewHolder holder, int order) {
			holder.setValues(s, order);
		}
	}
	
	

	class SubViewHolder {
		public static final int UN_POS = -1;
		public int mPos;
		public TextView order;
		public View operate;
		public TextView name;
		public TextView singer;
		public View insertV;
		public View delV;
		public View addV;

		public SubViewHolder() {
		}

		public void initView(View layout, OnClickListener click) {
			order = (TextView) layout.findViewById(R.id.order);
			name = (TextView) layout.findViewById(R.id.name);
			singer = (TextView) layout.findViewById(R.id.singer);
			operate = layout.findViewById(R.id.operate);
			insertV = layout.findViewById(R.id.insert);
			delV = layout.findViewById(R.id.del);
			addV = layout.findViewById(R.id.add);
			if (addV != null) {
				addV.setOnClickListener(click);
			}
			if (insertV != null) {
				insertV.setOnClickListener(click);
			}
			if (delV != null) {
				delV.setOnClickListener(click);
			}
			if (singer != null) {
				singer.setOnClickListener(click);
			}
		}

		public void setValues(Song data, int pos) {
			if (data == null) {
//				if (mPos != UN_POS && curSelView != null) {
//				if (curSelView != null) {
//					curSelView.setBackgroundDrawable(null);
//				}
					operate.setVisibility(View.GONE);
					order.setText(null);
					order.setSelected(false);
					singer.setText(null);
					singer.setSelected(false);
					name.setText(null);
					name.setSelected(false);
					delV.setTag(null);
//					delV.setBackgroundDrawable(null);
					delV.setSelected(false);
					mPos = UN_POS;
//				}
				return;
			}

			mPos = pos;
//			int page = pageManager.getCurrentIndex();
			int index = pos + 1;//page * PAGE_SIZE + pos + 1;
			if (index < 10) {
				order.setText("0" + index);
			} else {
				order.setText(String.valueOf(index));
			}
			name.setText(data.getName());
			singer.setText(data.getSingerName());
			singer.setTag(data.getSinger());
			if (insertV != null) {
				insertV.setTag(pos);
			}
			if (addV != null) {
				addV.setTag(data);
			}
//			delV.setBackgroundResource(R.drawable.del);
			delV.setTag(pos);
		}
	}

	public void onEvent(int event, Object obj, boolean isSuc) {
		switch (event) {
		case EventsManager.Event.SELCT_SONGS: {
			if (isRequestFirstSinging) {
				isRequestFirstSinging = false;
				if (isSuc && obj instanceof PageWrapper) {
					refreshTitle((PageWrapper) obj);
					int index = pageManager.getCurrentIndex();
					Player p = Player.G();
					int total = p.getPlaylistCount() - 1;
					int pageCount = total > 0 ? ((total - 1 ) / PAGE_SIZE + 1) : 0;

					if (index >= pageCount) {
						index = pageCount - 1;
					}
					if (index < 0) {
						index = 0;
					}
					loadingIntegerSet.clear();
//					pageManager.setPageInfo(index, pageCount, true);
					pageManager.setPageInfo(index, pageCount, false);
					update();

					// lst = pw.getData();
				}
			} else {
				if (isSuc && obj instanceof PageWrapper) {
					pageWrapper = (PageWrapper) obj;
					if (pageWrapper.getData() != null) {
						ArrayList<Song> lst = pageWrapper.getData();
						mPageCache.put(pageWrapper.getCurPage(), lst);
					}
					loadingIntegerSet.remove(pageWrapper.getCurPage());
					refreshPage();
				} else {
					loadingIntegerSet.clear();
				}
			}
		}
			break;
		case EventsManager.Event.DONE_SONGS:
//			if (isSuc && obj instanceof PageWrapper) {
//				Log.e(TAG, "suc");
//				pageWrapper = (PageWrapper) obj;
//				// lst = pw.getData();
//				refreshPage();
//			}
			if (isSuc && obj instanceof PageWrapper) {
				pageWrapper = (PageWrapper) obj;
				if (pageWrapper.getData() != null) {
					ArrayList<Song> lst = pageWrapper.getData();
					mPageCache.put(pageWrapper.getCurPage(), lst);
				}
				loadingIntegerSet.remove(pageWrapper.getCurPage());
				refreshPage();
			} else {
				loadingIntegerSet.clear();
			}
			break;
		case EventsManager.Event.SELCT_CHANG: {
			loadingIntegerSet.clear();
			mPageCache.clear();
			requestSelSongFirst();
			
//			requestSong(index, index * PAGE_SIZE, PAGE_SIZE);
			// requestSong(index, 0, PAGE_SIZE);
		}
			break;
		case EventsManager.Event.DONE_CHANG: {
			loadingIntegerSet.clear();
			mPageCache.clear();
			int index = pageManager.getCurrentIndex();
			Player p = Player.G();
			int total = p.getDonelistCount();
//			int pageCount = (total > 0 ? total - 1 : 0) / PAGE_SIZE + 1;
			int pageCount = total > 0 ? ((total - 1 ) / PAGE_SIZE + 1) : 0;

			if (index >= pageCount) {
				index = pageCount - 1;
			}
			if (index < 0) {
				index = 0;
			}
//			pageManager.setPageInfo(index, pageCount, true);
			pageManager.setPageInfo(index, pageCount, false);
			update();
//			requestSong(index, index * PAGE_SIZE, PAGE_SIZE);
		}
		break;
		default:
			break;
		}
		super.onEvent(event, obj, isSuc);
	}

	private boolean selectItem(View itemRoot) {
		if (curSelView == itemRoot) {
			return false;
		}
		if (curSelView != null) {
			SubViewHolder vhOld = (SubViewHolder) curSelView.getTag();
			curSelView.setBackgroundDrawable(null);
			vhOld.operate.setVisibility(View.GONE);
			vhOld.order.setSelected(false);
			vhOld.singer.setSelected(false);
			vhOld.name.setSelected(false);
			//vhOld.delV.setBackgroundResource(R.drawable.del_normal);
		}
		curSelView = itemRoot;
		if (curSelView != null) {
			SubViewHolder vh = (SubViewHolder) curSelView.getTag();
			if (vh != null && vh.mPos != SubViewHolder.UN_POS) {
				boolean isGone = vh.operate.getVisibility() != View.VISIBLE;
				curSelView.setBackgroundResource(R.drawable.choosing);
				vh.operate.setVisibility(View.VISIBLE);
				vh.order.setSelected(true);
				vh.singer.setSelected(true);
				vh.name.setSelected(true);
				//vh.delV.setBackgroundResource(R.drawable.del_pressed);
				
				return isGone;
			}
		}
		
		return false;
	}
	
	private void unselectItem() {
		if (curSelView != null) {
			SubViewHolder vhOld = (SubViewHolder) curSelView.getTag();
			curSelView.setBackgroundDrawable(null);
			vhOld.operate.setVisibility(View.GONE);
			vhOld.order.setSelected(false);
			vhOld.singer.setSelected(false);
			vhOld.name.setSelected(false);
			//vhOld.delV.setBackgroundResource(R.drawable.del_normal);
		}
	}

	public void onPageIndexChanged(int prePageIndex, int currentPageIndex,
			int totalPages) {
		if (type == TYPE_SEL && isRequestFirstSinging) {
			return;
		}
		
//		requestSong(currentPageIndex, currentPageIndex * PAGE_SIZE, PAGE_SIZE);
		int count = pageManager.getTotalPage();
		int startPos = 0;
		if (count > 0) {
			startPos = BASE_NUM / count * count;
		}
		startPos += currentPageIndex;
		listView.setSelection(startPos,true);
//		this.listView.setSelection(currentPageIndex, true);
		
		int total = Math.max(1, pageManager.getTotalPage());
		pageInoView.setText(getString(R.string.page_info, currentPageIndex % total + 1, total));
	}

	public void onPageIndexChangedError(int prePageIndex, int currentPageIndex,
			int totalPages) {
	}

	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		int total = Math.max(1, pageManager.getTotalPage());

//		pageInoView.setText(getString(R.string.page_info, position % total + 1, total));
		pageManager.setCurrentIndex(position % total, false);
	}

	public void onNothingSelected(AdapterView<?> parent) {
		// TODO Auto-generated method stub
		
	}

	private float oldX;
	private final float diff = 10;
//	private int[] location = new int[2];
	private Rect rect2 = new Rect();
	private long clickTime = 0;
//	private boolean isDraging;
	
	public void onMyTouchListner(float x, float y, int e) {
		if (e == HListView.MyTouchListner.EVENT_DOWN) {
			if (clickTime == 0) {
				clickTime = System.currentTimeMillis();
			}
			oldX = x;
			return;
		} 
		
		// 取消拖动
//		else if (e == HListView.MyTouchListner.EVENT_MOVE && (type == TYPE_SEL) ) {
//			if (Math.abs(oldX - x) < diff) {
//			if (clickTime > 0 && System.currentTimeMillis() - clickTime > 1200 && ! isDraging) {
//			View root = listView.getSelectedView();
//			if (root == null || root.getTag() == null
//					|| root.getVisibility() != View.VISIBLE) {
//				return;
//			}
//			ViewHolder vh = (ViewHolder) root.getTag();
//			int index = (((int) (y - 145)) / vh.rows[0].getHeight())
//			% PAGE_SIZE;
//			int pos[] = new int[2];
//			mLongClickPos  = index;
////			selectItem(view);
//			vh.rows[index].getLocationInWindow(pos);
//			mDragView.startDrag(vh.rows[index], pos[0], pos[1]);
//			isDraging = true;
//			}
//			}
//		return;
//		}
		if (e == HListView.MyTouchListner.EVENT_UP 
				|| e == HListView.MyTouchListner.EVENT_CANCEL) {
			clickTime = 0;
//			isDraging = false; // 取消拖动
			if (Math.abs(oldX - x) < diff) {
				View root = listView.getSelectedView();
				if (root == null || root.getTag() == null
						|| root.getVisibility() != View.VISIBLE) {
					return;
				}
				Log.e("SongListActivity:", "y:" + y);
				ViewHolder vh = (ViewHolder) root.getTag();
				int index = (((int) (y - 150)) / vh.rows[0].getHeight())
						% PAGE_SIZE;
				Object suTag = vh.rows[index].getTag();
				/*
				if (suTag instanceof SubViewHolder && !isClickedSinger) {
					SubViewHolder subHodler = (SubViewHolder) suTag;
					subHodler.singer.getGlobalVisibleRect(rect2);
					if (rect2.contains((int)x, (int)y) ) {
						isClickedSinger = true;
						StarSongsActivity.starActivity(SelSongListActivity.this,
								((TextView) subHodler.singer).getText()
										.toString());
						return;
					}
					if (subHodler.delV != null
							&& subHodler.delV.getVisibility() == View.VISIBLE) {
						subHodler.delV.getGlobalVisibleRect(rect2);
						if (rect2.contains((int) x, (int) y)) {
							if (subHodler.delV.getTag() instanceof Integer) {
								unselectItem();
								int pos = (Integer) subHodler.delV.getTag();
								// int page = pageManager.getCurrentIndex();
								// int delIndex = page * PAGE_SIZE + pos + 1;
								int delIndex = pos + 1;
								XVod.G().deleteSong(delIndex);
								return;
							}
						}
					}
				}
				*/
//				selectItem(vh.rows[index]);
			}
			if (e == HListView.MyTouchListner.EVENT_UP) {
				mHandle.removeMessages(1);
				mHandle.sendEmptyMessageDelayed(1, 200);
			}
		}
	}
	
	private Handler mHandle = new Handler() {

		@Override
		public void handleMessage(Message msg) {
//			super.handleMessage(msg);
			int total = Math.max(1,pageManager.getTotalPage());
			int position = listView.getSelectedItemPosition();
			if (position >= 0) {
				pageInoView.setText(getString(R.string.page_info, position % total + 1, total));
			}

			
		}
		
	};
	

}
